package com.sky.es.service.impl;

import com.sky.es.entity.ProductDoc;
import com.sky.es.service.ProductSuggQueryService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author yangsk
 */
@Service
@Slf4j
public class ProductSuggQueryServiceImpl implements ProductSuggQueryService {
    @Autowired
    private ElasticsearchRestTemplate esTemplate;

    @Override
    public List<ProductDoc> queryBySugg(String sugg) {

        CompletionSuggestionBuilder suggQuery = SuggestBuilders.completionSuggestion("suggestionText");
        suggQuery.prefix(sugg).skipDuplicates(true);

        SuggestBuilder suggBuilder = new SuggestBuilder();
        suggBuilder.addSuggestion("suggestionTest",suggQuery);
        esTemplate.suggest(suggBuilder, ProductDoc.class);
        return Collections.emptyList();
    }
}
